package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*
    执行关联查询
 */
public class JDBCDemo6 {
    public static void main(String[] args) {
        //王克晶所带班级的信息,列出老师的名字,老师的性别,班级名,所在楼层
        try(
                Connection connection = DBUtil.getConnection();
                ){
            Statement state = connection.createStatement();
            String sql="SELECT t.name,t.gender,c.name,c.floor "+
                    "FROM teacher t "+
                    "JOIN class c ON t.id =c.teacher_id "+
                    "WHERE t.name ='王克晶' " +
                    "" +
                    "";
            ResultSet res = state.executeQuery(sql);
            while (res.next()){
                String tname =res.getString("t.name");
                String tgender =res.getString("t.gender");
                String cname =res.getString("c.name");
                int cfloor = res.getInt("c.floor");
                System.out.println(tname+","+tgender+","+cname+","+cfloor);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }
}
